pacman::p_load(ggtern, dplyr, ggplot2, lazyopt, magrittr)

arg <- c("-i", "E:/projects/R_draw_sc/test/sanyuan/data/Ternary.groupS-TB.combine.xls2",
         "-o", "E:/projects/R_draw_sc/test/sanyuan/data/",
         "-pcs", "grey:red:blue:green"
)

opt <- matrix(c("tablepath", "i", 2, "character", "The path to the table data read", "",
                "outputfilepath", "o", 2, "character", "The package path of the output image", "",
                "point_colorstyle", "pcs", 1, "character", "Set the color to ':' split", "#CD0000:#3A89CC:#769C30:#D99536:#7B0078:#BFBC3B:#E2609F:#00688B:#C10077",
                "pointsize", "psz", 1, "numeric", "set point size", "2",
                "mainlab", "ml", 1, "character", "set mainlab,split by [:] name:position", " :0.42",
                "xlab", "xl", 1, "character", "set xlab", " ",
                "ylab", "yl", 1, "character", "set ylab", " ",
                "zlab", "zl", 1, "character", "set zlab ", " ",
                "legendlab", "ll", 1, "character", "set legend title", " ",
                "imageSize", "is", 1, "character", "The height and width of the picture", "NULL:NULL",
                "resolution", "dpi", 1, "numeric", "Set the resolution to allow 72,96,300 or 600", "300",
                "imageformt", "if", 1, "character", "pdf,tiff,png", "png",
                "imageName", "in", 1, "character", "picture name", "sanyuan",
                "help", "h", 0, "numeric", "Help document", "1"
), byrow = TRUE, ncol = 6) %>%
  lazyopt(arg = arg)

resolution <- match.arg(opt %$%
                          as.character(resolution),
                        c("72", "96", "300", "600")) %>%
  as.numeric()


data <- opt %$%
  read.delim(tablepath)

names <- data %>%
  colnames()
names <- names[2:5]

if (opt$xlab != " ") {
  names[1] <- opt$xlab
}

if (opt$ylab != " ") {
  names[2] <- opt$ylab
}

if (opt$zlab != " ") {
  names[3] <- opt$zlab
}

if (opt$legendlab != " ") {
  names[4] <- opt$legendlab
}

main <- opt %$%
  fenge(mainlab)


if (opt$legendlab == " ") {
  opt$legendlab <- names[4]
}

point_colorstyle <- opt %$%
  fenge(point_colorstyle)

point_colorstyle <- point_colorstyle[data[, 4] %>%
                                       unique() %>%
                                       seq_along]

imageSize <- opt %$%
  fenge(imageSize)
width <- NA
height <- NA
if (imageSize[1] != "NULL") {
  width <- imageSize[1] %>%
    as.numeric()
}
if (imageSize[2] != "NULL") {
  height <- imageSize[2] %>%
    as.numeric()
}

size <- (apply(data[2:4], 1, mean))^0.5

(ggtern(data,
        aes(x = data[, 2], y = data[, 3], z = data[, 4], color = data[, 5], group = data[, 5])) +
  theme_rgbw() +
  geom_point(aes(size = size)) +
  labs(x = names[1], y = names[2], z = names[3], title = main[1], color = opt$legendlab) +
  scale_color_manual(values = point_colorstyle) +
  scale_size(range = c(0,6))+
  theme(plot.title = element_text(hjust = main[2] %>%
    as.numeric()
  )
  )
) %>%
  ggsave(filename = opt %$%
    paste0(outputfilepath, "/", imageName, ".", imageformt),
         width = width, height = height, dpi = resolution)









